from flask import Blueprint, jsonify, url_for
from flask_login import login_required
from demo import mysql_demo, redis_demo, baidu_ocr


def init_app(app, prefix='/demo'):

    demo = Blueprint('demo', __name__, url_prefix=prefix)

    @demo.route('/')
    def index():
        return 'in demo module %s' % url_for('.index')

    @demo.route('/test', methods=['GET', 'POST'])
    def test():
        return 'demo test'

    @demo.route('/ocr')
    def ocr():
        return jsonify(baidu_ocr.test_test())

    @demo.route("/db")
    @login_required
    def db():
        return jsonify(mysql_demo.test_db(None))

    @demo.route("/wdb")
    def wdb():
        return jsonify(mysql_demo.write_db())

    @demo.route("/up")
    def update_mysql():
        return jsonify(mysql_demo.update_db())

    @demo.route("/db/<count>")
    def do_mysql2(count):
        return jsonify(mysql_demo.test_db(count))

    @demo.route("/redis")
    def do_redis():
        return jsonify(redis_demo.test_redis())

    app.register_blueprint(demo)